package us.mooresr.webtroopmanager.client.ui;

import java.util.List;

import us.mooresr.webtroopmanager.client.place.AdminPlace;
import us.mooresr.webtroopmanager.client.place.EditRolePlace;
import us.mooresr.webtroopmanager.client.ui.cells.RoleCell;
import us.mooresr.webtroopmanager.shared.Role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class RoleListViewImpl extends Composite implements RoleListView {
	private Presenter presenter;
	private Role selectedRole;
	private ListDataProvider<Role> dataProvider;
	private CellList<Role> cellList;
	
	private static RoleListViewImplUiBinder uiBinder = GWT
			.create(RoleListViewImplUiBinder.class);

	interface RoleListViewImplUiBinder extends
			UiBinder<Widget, RoleListViewImpl> {
	}

	@UiField
	Button addButton;
	
	@UiField
	Button editButton;
	
	@UiField
	Button deleteButton;
	
	@UiField
	Button backButton;
	
	@UiField
	ScrollPanel scrollPanel;
	
	@UiField
	Label statusLabel;
	
	@UiHandler("addButton")
	void onAddClick(ClickEvent e) {
		presenter.goTo(new EditRolePlace(null));
	}
	
	@UiHandler("editButton")
	void onEditClick(ClickEvent e) {
		presenter.goTo(new EditRolePlace(selectedRole.getId()));
	}
	
	@UiHandler("deleteButton")
	void onDeleteClick(ClickEvent e) {
		presenter.deleteRole(selectedRole);
	}
	
	@UiHandler("backButton")
	void onBackClick(ClickEvent e) {
		presenter.goTo(new AdminPlace());
	}

	public RoleListViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		ProvidesKey<Role> keyProvider = new ProvidesKey<Role>() {
			public Object getKey(Role item) {
				return (item == null) ? null : item.getId();
			}
		};
		
		cellList = new CellList<Role>(new RoleCell(), keyProvider);

		cellList.setSize("292px", "172px");
		
		final SingleSelectionModel<Role> selectionModel = new SingleSelectionModel<Role>(keyProvider);
		cellList.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				selectedRole = selectionModel.getSelectedObject();
				if (selectedRole == null) {
					// Disable buttons that act on a selected event
					editButton.setEnabled(false);
					deleteButton.setEnabled(false);
	
				} else {
					// Enable buttons that act on a selected event
						editButton.setEnabled(true);
						deleteButton.setEnabled(true);

				}
				
				
			}
			
		});
		
		dataProvider = new ListDataProvider<Role>();
		dataProvider.addDataDisplay(cellList);
		
		scrollPanel.setWidget(cellList);
		cellList.setSize("100%", "100%");
		
		deleteButton.setEnabled(false);
		editButton.setEnabled(false);

	}
	

	
	@Override
	public void addRole(Role e) {
		List<Role> providerList = dataProvider.getList();
		
		providerList.add(e);
		
		// TODO: Resort?
		
		cellList.setPageSize(providerList.size());
		
	}

	@Override
	public void removeRole(Long id) {
		List<Role> providerList = dataProvider.getList();
		
		for (int i=0; i<providerList.size(); i++) {
			Role es = providerList.get(i);
			if (es.getId().equals(id)) {
				providerList.remove(i);
				break;
			}
		}
		
	}

	@Override
	public void setData(List<Role> data) {

		List<Role> providerList = dataProvider.getList();
		providerList.clear();
		for (Role es : data) {
			providerList.add(es);
		}
		cellList.setPageSize(data.size());
		
	}

	@Override
	public void setStatus(String status) {
		statusLabel.setText(status);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}

}
